﻿

<?php

session_start();
include_once( 'config.php' );
include_once( 'weibooauth.php' );



$o = new WeiboOAuth( WB_AKEY , WB_SKEY , $_SESSION['keys']['oauth_token'] , $_SESSION['keys']['oauth_token_secret']  );

$last_key = $o->getAccessToken(  $_REQUEST['oauth_verifier'] ) ;

$_SESSION['last_key'] = $last_key;
//put $lastkey into database  $last_key['oauth_token'], $last_key['oauth_token_secret']






//************登录时，把用户oauth_token,oauthen_secret写到数据库**************
$c = new WeiboClient( WB_AKEY , WB_SKEY , $_SESSION['last_key']['oauth_token'] , $_SESSION['last_key']['oauth_token_secret']  );
$me = $c->verify_credentials();  //取用户名等个人信息

$name=$me[name]; //由于不能直接用右边的内容，所以用了个变量作为中介。
$id=$me[id];  
$user_url = "http://weibo.com/".$me[id];
$profile_image_url=$me[profile_image_url]; 
$oauth_token = $_SESSION['last_key']['oauth_token'] ;
$oauth_token_secret = $_SESSION['last_key']['oauth_token_secret'];

$con = mysql_connect("localhost", "root","");   
mysql_query("SET NAMES utf8");   //下面3行是解决sql写入写出显示乱码的，详细见http://wenwen.soso.com/z/q104200853.htm
mysql_query("SET CHARACTER SET utf8");
mysql_query("SET COLLATION_CONNECTION='utf8_general_ci'");

if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
mysql_select_db("movie3", $con);      //先选择数据库
 $sql = "SELECT * FROM member WHERE weibo_uid='$id' "; 
 $result = mysql_query($sql,$con);         //执行上面一行的sql		
 	
 $timestamp = time();
 if (mysql_num_rows($result))              //判断是否该用户id存在，即是否有结果记录
	  { 
	 $sql = " update member SET username='$name', weibo_name = '$name', oauth_token = '$oauth_token', oauth_token_secret = '$oauth_token_secret', user_url='$user_url', profile_image_url='$profile_image_url' , lasttime='$timestamp' where weibo_uid = '$id' " ;  //更新信息
	  }
  else
	   { 
	  $sql = " INSERT INTO member ( weibo_uid, username, weibo_name, user_url, profile_image_url, oauth_token, oauth_token_secret, regtime )  VALUES ( '$id', '$name', '$name', '$user_url', '$profile_image_url', '$oauth_token', '$oauth_token_secret', '$timestamp '  )" ;     //插入新用户数据
	 	  }
mysql_query($sql,$con); 


 $sql = "SELECT * FROM member WHERE weibo_uid='$id' ";   //把值赋给session,以便后面程序取用
 $result = mysql_query($sql,$con);         
 $row=mysql_fetch_array($result);
 				$_SESSION['username']	= $row['username'];
				$_SESSION['uid'] = $row['uid'];
				$_SESSION['weibo_uid'] = $row['weibo_uid'];

//************登录时，把用户oauth_token,oauthen_secret写到数据库**************





//echo "token".$_SESSION['last_key']['oauth_token'] ."----". $_SESSION['last_key']['oauth_token_secret']. $_SESSION['username'];

//echo '成功登录...返回上一页'.'<script language="javascript">history.back(-1);</script>';

?>


授权完成, 自动进入网站首页...
<meta http-equiv="refresh" content="2;url=http://www.movie3.com/">  

